package com.qdxwx.manage.controller.course;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/course")
public class CourseSelectController {

    @Autowired
    private CourseService courseService;

    //添加课程页面
    @GetMapping("/select.html")
    public String courseSelect() {
        return "course/course-select";
    }

    /**
     * 查询课程列表
     */
    @PostMapping("/select/list.json")
    @ResponseBody
    public TableDataInfo courseList(HttpServletRequest request) {
        String courseName = TKString.trim(request.getParameter("courseName"));
        CourseQuery course = new CourseQuery();
        if (courseName.length() > 0) {
            course.setCourseName(courseName);
        }
        course.setIsAvailable(1);
        List<Course> list = courseService.getList(course, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(courseService.count(course));
        return rspData;
    }
}
